<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/logic/LogicHoC.class.php');
require_once(SITE_ROOT . 'lib/Session.php');

	$logic = LogicHoC::getInstance();
	$smarty = new SmartyMaptrick();
	
	if ($_POST['select_alliances'] == 0) {
		$display_input = true;
		$allianceID = $_POST['input_alliance'];
	}
	else {
		$display_input = false;
		$allianceID = $_POST['select_alliances'];
	}	
	
	$hidden = $_POST['hidden'];
	
	if ($hidden == 'login') {
		try {
			$register = new Registration;
			$register->login($_POST['user'], $_POST['password']);
		}
		catch (Exception $e) {
			$smarty->assign('message', $e->getMessage());
		}
	}
	else if ($hidden == 'logout') {
		try {
			$register = new Registration;
			$register->logout();
		}
		catch (Exception $e) {}
	}
	else if ($hidden == 'fetch') {
		try {
			if (preg_match("/^[0-9]{1,7}$/", $allianceID)) {
				$alliance = $logic->fetchAlliance($allianceID);
			}
			else $smarty->assign('error', "The AllianceID isn't valid! It may only contain numbers.");
		}
		catch (Exception $e) {
			$errorCode = substr($e->getMessage(), strpos($e->getMessage(), "(") + 1, strpos($e->getMessage(), ")") - strpos($e->getMessage(), "(") - 1);
			switch ($errorCode) {
				case 0: {
					unset($_SESSION['login']);
					unset($_SESSION['login_teamID']);
					unset($_SESSION['HT']);
					$smarty->assign('message', 'Timeout!');
					break;
				}
				case 99: {
					$smarty->assign('error', "You aren't a member of this federation and may not import/update it!");
					break;
				}
				default: $smarty->assign('error', "An error has occured! Please try again later.");
			}			
		}
	}
		
	$smarty->assign('action', $_SERVER['PHP_SELF']);
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('home_active', '');
	$smarty->assign('cool_active', '');
	$smarty->assign('hoc_active', 'active');
	
	$smarty->assign('introduction', "<p>You can import a Hattrick federation here if you are a member of that federation. You have to be logged in to do anything here. To import a federation choose \"Enter AllianceID manually...\" and enter the ID of your federation in the textfield which appears then. To update a already imported federation just choose it from the dropdown box and press \"Fetch\" afterward. If everything was successfull a link to the Hall of Cool is presented to you where you can update all the members of the federation then.</p>");
	
	$smarty->assign('selected_alliance', $allianceID);
	$smarty->assign_by_ref('alliances', $logic->getAlliances());
	
	if ($alliance != null) {
		$display_input = false;
		$smarty->assign('info', "The federation \"" . $alliance->getName() . "\" (" . $alliance->getAllianceID() . ") was imported/updated successfully. It currently has " . $alliance->getNumberMembers() . " members. You can take a look at the federation in the Hall of Cool by clicking <a href=\"hallofcool.php?alliance=" . $alliance->getAllianceID() . "\">here</a>.");
	}
	
	$smarty->assign('display_input', $display_input);
	
	// display it
	$smarty->display('header.tpl');
	$smarty->display('fedimport.tpl');	
	$smarty->display('footer.tpl');
	
?>